﻿namespace Example.Persistence
{
    using Example.Persistence.Account;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Data.Entity.Config;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Validation;
    using System.Diagnostics.Contracts;
    using System.Linq;

    /// <summary>
    /// Implements functionality to persist entities.
    /// </summary>
    public class Entities : DbContext, IEntities
    {
        static Entities()
        {
            Database.SetInitializer(new EntitiesInitializer());
        }

        /// <summary>
        /// The cached sets.
        /// </summary>
        private readonly IDictionary<Type, object> sets = new Dictionary<Type, object>();

        /// <summary>
        /// Gets a set of entitites.
        /// </summary>
        /// <typeparam name="T">Type of the entities.</typeparam>
        /// <returns>
        /// The set of entities.
        /// </returns>
        public IEntitySet<TEntity> Of<TEntity>() where TEntity : class
        {
            return new EntitySet<TEntity>(this.Set<TEntity>());
        }

        IQueryable<T> IQueryableEntities.Of<T>()
        {
            return this.Of<T>();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>();
        }
    }

    public class EntitiesInitializer : DropCreateDatabaseIfModelChanges<Entities>
    {
        protected override void Seed(Entities context)
        {

        }
    }
}
